package web.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * @description 发送
 * @className UdpSendDemo
 * @package web.udp
 * @author dongww@hsyuntai.com
 * @date 2020/3/10 16:38
 * @copyright 版权归 HSYUNTAI 所有
 */
public class UdpSendDemo {

    public static void main(String[] args) throws SocketException, UnknownHostException, IOException {
        DatagramSocket socket = new DatagramSocket();

        String msg = "this is a test---2";

        InetAddress address = InetAddress.getByName("DESKTOP-61K2GK2");
        int port = 456;

        //打包
        DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.getBytes().length, address, port);

        //发送数据
        socket.send(packet);
        socket.close();
    }
}